package com.gzhu.knowledgeAdmin.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 返回工具类
 *
 *
 */
@NoArgsConstructor
@Data
public class R<T> implements Serializable {

    private int code;

    private T data;

    private String message;

    private R(int code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    /**
     * 成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> R<T> ok(T data) {
        return new R<>(ErrorCode.SUCCESS.getCode(), data, "ok");
    }


    public static <T> R<T> ok() {
        return new R<>(ErrorCode.SUCCESS.getCode(), null, "ok");
    }

    /**
     * 失败
     *
     * @param errorCode
     * @return
     */
    public static <T> R<T> error(ErrorCode errorCode) {
       return new R<>(errorCode.getCode(),null,errorCode.getMessage());
    }


    /**
     * 默认的错误是系统错误
     * @param <T>
     * @return
     */
    public static <T> R<T> error() {
        return new R<>(ErrorCode.SYSTEM_ERROR.getCode(), null,ErrorCode.SYSTEM_ERROR.getMessage());
    }

    public static <T> R<T> error(int code,String message) {
        return new R<>(code,null,message);
    }

}
